package cn.edu.zut.curriculum.controller;

import cn.edu.zut.curriculum.entity.Commodity;
import cn.edu.zut.curriculum.entity.User;
import cn.edu.zut.curriculum.service.IAdministratorService;
import cn.edu.zut.curriculum.service.ICommodityService;
import cn.edu.zut.curriculum.service.IUserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class AdministratorController {
    @Autowired
    IAdministratorService iAdministratorService;
    @Autowired
    IUserService iUserService;
    @Autowired
    ICommodityService iCommodityService;

    @GetMapping("/administratorlist")
    public ModelAndView skip1(ModelAndView mav,@RequestParam("uid") long uid){
        User user=iUserService.select(uid);
        mav.addObject("administrator",user);
        mav.setViewName("User/administratorlist");
        return mav;
    }


    @GetMapping ("/commonlist")
    public ModelAndView select(ModelAndView mav,@RequestParam("uid") long id){
        User administrator=iUserService.select(id);
       // System.out.println("wcnm");
        List<User> userlist =iAdministratorService.select();
        mav.addObject("userlist",userlist);
        mav.addObject("administrator",administrator);
        mav.setViewName("/User/commonlist");
        return mav;
    }

    @PostMapping("/change")  //管理员禁言操作
    public ModelAndView change(ModelAndView mav,@RequestParam("id")long id,@RequestParam("uid") long uid){
        System.out.println(id);
        int i=iUserService.update(id);
        int b=iCommodityService.update2(id);
        User user=iUserService.select(uid);
        System.out.println(b);
        if(i==1)
            mav.addObject("message","修改成功");
        else
            mav.addObject("message","未知错误");
        mav.addObject("administrator",user);
        mav.setViewName("/User/administratorlist");
        return mav;
    }

    @GetMapping ("/commoditycontrol")
    public ModelAndView commoditycontrol(ModelAndView mav,@RequestParam("uid") long uid){
        User user=iUserService.select(uid);
        List<Commodity> list= iCommodityService.select5(uid);
        mav.addObject("list",list);
        mav.addObject("administrator",user);
        mav.setViewName("/User/commoditycontrol");
        return mav;
    }


    @PostMapping("/down")  //管理员下架操作
    public ModelAndView down(ModelAndView mav,@RequestParam("cid")long cid,@RequestParam("uid") long uid) {
        User user=iUserService.select(uid);
        int i=iCommodityService.delete(cid);
        if(i==1){
            mav.addObject("message","已成功下架");

        }
        else
            mav.addObject("message","未知异常");


        mav.addObject("administrator",user);
        mav.setViewName("/User/administratorlist");
        return mav;
    }
}



